package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.entity.TbTaskAttack;
import com.southwind.entity.TbTaskScan;
import com.southwind.form.SearchForm;
import com.southwind.mapper.TbIfadminMapper;
import com.southwind.mapper.TbTaskAttackMapper;
import com.southwind.mapper.TbTaskScanMapper;
import com.southwind.service.TbTaskScanService;

import com.southwind.vo.PageVO;
import com.southwind.vo.TbScanVO;
import com.southwind.vo.TbTaskAttackVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fjh
 * @since 2024-01-11
 */
@Service
public class TbTaskScanServiceImpl extends ServiceImpl<TbTaskScanMapper, TbTaskScan> implements TbTaskScanService {
@Autowired
private TbTaskScanMapper tbTaskScanMapper;
@Autowired
private TbTaskAttackMapper tbTaskAttackMapper;
@Autowired
private TbIfadminMapper tbIfadminMapper;

    @Override
    public PageVO list(Integer page, Integer size) {
        Page<TbTaskScan>  tbTaskScanPage= new Page<>(page, size);
        Page<TbTaskScan> resultPage = this.tbTaskScanMapper.selectPage(tbTaskScanPage, null);


        List<TbScanVO> tbScanVOList = new ArrayList<>();
        for (TbTaskScan tbTaskScan : resultPage.getRecords()) {
            TbScanVO tbScanVO = new TbScanVO();
            BeanUtils.copyProperties(tbTaskScan, tbScanVO);
//            设置关联任务名
         tbScanVO.setTaskName(this.tbTaskAttackMapper.selectById(tbTaskScan.getMytaskId()).getTaskName());
//          设置测试人
            tbScanVO.setTester(this.tbIfadminMapper.selectById(tbTaskScan.getTester()).getUsername());

            tbScanVOList.add(tbScanVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbScanVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
//        模糊查询+分页
        Page<TbTaskScan> tbTaskScanPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<TbTaskScan> resultPage = null;
        if(searchForm.getValue().equals("")){
            resultPage = this.tbTaskScanMapper.selectPage(tbTaskScanPage, null);
        } else {
            QueryWrapper<TbTaskScan> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.tbTaskScanMapper.selectPage(tbTaskScanPage,queryWrapper);
        }
        List<TbScanVO> tbScanVOList = new ArrayList<>();
        for (TbTaskScan tbTaskScan : resultPage.getRecords()) {
            TbScanVO tbScanVO = new TbScanVO();
            BeanUtils.copyProperties(tbTaskScan, tbScanVO);
//            设置关联任务名
            tbScanVO.setTaskName(this.tbTaskAttackMapper.selectById(tbTaskScan.getMytaskId()).getTaskName());
//          设置测试人
            tbScanVO.setTester(this.tbIfadminMapper.selectById(tbTaskScan.getTester()).getUsername());

            tbScanVOList.add(tbScanVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbScanVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }
}
